package com.erp.erp_ui.Product.controller;

import com.erp.erp_entitys.ProductBoxGauge.ProductBoxGauge;
import com.erp.erp_servers.ProductBoxGauge.IProductBoxGaugeService;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-09-12 18:16
 */
@Slf4j
@Controller
@RequestMapping("boxGauge")
public class ProductBoxGaugeController {

    private final IProductBoxGaugeService boxGaugeService;


    public ProductBoxGaugeController(IProductBoxGaugeService boxGaugeService) {
        this.boxGaugeService = boxGaugeService;
    }

    /**
     * 产品箱规-保存
     * @param boxGauge
     * @return
     */
    @RequestMapping(value = "saveBoxGauge")
    @ResponseBody
    public R saveBoxGauge(@RequestBody ProductBoxGauge boxGauge){
        try{
            Integer num;
            if (boxGauge.getId()==null){
                num =boxGaugeService.insertSelective(boxGauge);
            }else {
                num =boxGaugeService.updateByParam(boxGauge);
            }
            if (num!=0 || num != null){
                return R.build(ResultStatusEnum.SUCCESS);
            }else {
                return R.buildError(ResultStatusEnum.DATA_ERROR);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 产品箱规-查询
     * @return
     */
    @RequestMapping(value = "findBoxGaugeListByPage")
    @ResponseBody
    public R findBoxGaugeList(String productCode,String specificationCode){
        try{
            if (StringUtils.isEmpty(productCode) && StringUtils.isEmpty(specificationCode)){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<ProductBoxGauge> pageInfo =boxGaugeService.findBoxGaugeByParam(productCode,specificationCode);
            return R.build(200, "success", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 产品箱规-删除
     * @return
     */
    @RequestMapping(value = "delBox")
    @ResponseBody
    public R saveBoxGauge(@RequestParam("ids") List<Integer> ids){
        try{
            if (ids.size()<=0){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            boxGaugeService.deleteByIds(ids);
            return R.build(ResultStatusEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

}
